#include "SFMLSoundManager.h"


SFMLSoundManager::SFMLSoundManager(void)
{
	m_dataFilepath.setPath("audio.dat");
	m_dataFilepath.parse();
}


SFMLSoundManager::~SFMLSoundManager(void) { m_sounds.clear(); }

vn::SFMLSound* SFMLSoundManager::loadSound(const std::string& p_id)
{
	vn::SFMLSound* sound = new vn::SFMLSound;
	auto it = m_sounds.find(p_id);
	if (it != m_sounds.end())
	{
		sound->setSoundBuffer(it->second);
	}
	else
	{
		std::string filepath = m_dataFilepath.getFilepath(p_id);
		sf::SoundBuffer buffer;
		buffer.loadFromFile(filepath);
		m_sounds.insert(std::make_pair(p_id, buffer));
		sound->setSoundBuffer(m_sounds[p_id]);
	}
	return sound;
}

vn::SFMLMusic* SFMLSoundManager::loadMusic(const std::string& p_filepath)
{
	vn::SFMLMusic* music = new vn::SFMLMusic;
	music->setMusicFile(p_filepath);
	return music;
}

void SFMLSoundManager::destroyAudioBuffer(const std::string& p_id)
{
	auto it = m_sounds.find(p_id);
	if (it != m_sounds.end())
	{
		m_sounds.erase(it);
	}
}

void SFMLSoundManager::clear()
{
	m_sounds.clear();
}